/*
 * Lenovo Group
 * Copyright (c) 2015-2016 All Rights Reserved.
 * 
 * Project Name:assign-tracer
 * Create Time: 2016年2月24日 下午9:02:45
 */
package com.lenovo.lmrp.server.assigntracer.util.grap;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.lenovo.lmrp.api.model.entity.TicketProcessPO;
import com.lenovo.lmrp.api.model.view.ChangedTicketVO;
import com.lenovo.lmrp.api.model.view.GrabRecode;
import com.lenovo.lmrp.server.assigntracer.repository.ARepositoryFactory;

/**
 * First come first served, grab a single strategy
 * 
 * @author songyz
 * @version $Id: FirstGrab.java, v 0.1 2016年2月24日 下午9:02:45 songyz Exp $
 */
public class FCFSGrab implements IGrab {

	private static final long serialVersionUID = 1L;
	private final static Logger logger = LoggerFactory.getLogger(FCFSGrab.class);
	private Set<GrabRecode> set = new HashSet<>();

	// 将抢单记录放入队列中
	public void put(GrabRecode grabRecode) {
		if (set.size() == 0) {
			// 这就是先到的工程师
			if (logger.isDebugEnabled())
				logger.debug("The first engineer of grab engineer list is " + grabRecode.getEngineerId());

			System.out.println(grabRecode.getEngineerId() + "是第一个抢：" + grabRecode.getTicketId());

			TicketProcessPO processPO = ARepositoryFactory.getFactory().getWorkflowRepository()
					.getRuningTicketProcess(grabRecode.getItsmCode(), grabRecode.getTicketId());
			Map<String, String> params2 = new HashMap<>();
			params2.put("choicedEngineer", grabRecode.getEngineerId());
			ChangedTicketVO cvo = new ChangedTicketVO();
			cvo.setAction("grab");
			cvo.setFields(params2);
			ARepositoryFactory.getFactory().getIProcessService().fireProcessInstance(processPO, cvo);
		}
		set.add(grabRecode);
	}

}
